import type {VbenFormSchema} from '#/adapter/form';
import type {VxeTableGridOptions} from '#/adapter/vxe-table';
import type {
  AcquisitionAgriculturalMachineryOperationApi
} from '#/api/acquisition/agriculturalmachineryoperation';

import {getRangePickerDefaultProps} from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'licensePlate',
      label: '车牌号',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入车牌号',
      },
    },
    {
      fieldName: 'driverName',
      label: '司机姓名',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入司机姓名',
      },
    },
    {
      fieldName: 'driverPhone',
      label: '司机电话',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入司机电话',
      },
    },
    {
      fieldName: 'operationDate',
      label: '作业日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'operationArea',
      label: '作业面积（亩）',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入作业面积（亩）',
      },
    },
    {
      fieldName: 'qualifiedArea',
      label: '达标面积（亩）',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入达标面积（亩）',
      },
    },
    {
      fieldName: 'projectArea',
      label: '项目面积（亩）',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入项目面积（亩）',
      },
    },
    // 上报年月字段在表单中不显示
    {
      fieldName: 'reportingYearMonth',
      label: '上报年月（格式：YYYY-MM）',
      component: 'Input',
      componentProps: {
        placeholder: '请输入上报年月（格式：YYYY-MM）',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  // 获取当前年月，格式为 YYYY-MM
  const currentDate = new Date();
  const currentYearMonth = `${currentDate.getFullYear()}-${String(currentDate.getMonth() + 1).padStart(2, '0')}`;
  
  return [
    {
      fieldName: 'licensePlate',
      label: '车牌号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入车牌号',
      },
    },
    {
      fieldName: 'driverName',
      label: '司机姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入司机姓名',
      },
    },
    {
      fieldName: 'driverPhone',
      label: '司机电话',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入司机电话',
      },
    },
    {
      fieldName: 'operationDate',
      label: '作业日期',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'operationArea',
      label: '作业面积（亩）',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入作业面积（亩）',
      },
    },
    {
      fieldName: 'qualifiedArea',
      label: '达标面积（亩）',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入达标面积（亩）',
      },
    },
    {
      fieldName: 'projectArea',
      label: '项目面积（亩）',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入项目面积（亩）',
      },
    },
    {
      fieldName: 'reportingYearMonth',
      label: '上报年月（格式：YYYY-MM）',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入上报年月（格式：YYYY-MM）',
        defaultValue: currentYearMonth,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AcquisitionAgriculturalMachineryOperationApi.AgriculturalMachineryOperation>['columns'] {
  return [
    {type: 'checkbox', width: 40},
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
    },
    {
      field: 'licensePlate',
      title: '车牌号',
      minWidth: 120,
    },
    {
      field: 'driverName',
      title: '司机姓名',
      minWidth: 120,
    },
    {
      field: 'driverPhone',
      title: '司机电话',
      minWidth: 120,
    },
    {
      field: 'operationDate',
      title: '作业日期',
      minWidth: 120,
    },
    {
      field: 'operationArea',
      title: '作业面积（亩）',
      minWidth: 120,
    },
    {
      field: 'qualifiedArea',
      title: '达标面积（亩）',
      minWidth: 120,
    },
    {
      field: 'projectArea',
      title: '项目面积（亩）',
      minWidth: 120,
    },
    {
      field: 'reportingYearMonth',
      title: '上报年月（格式：YYYY-MM）',
      minWidth: 120,
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: {default: 'actions'},
    },
  ];
}